<?php

class Fe_adv_model extends CI_Model {

    function __construct() {
        parent::__construct();
    }

    function _search_adv_list($keyword, $cate_id, $city_id, $limit_from, $limit_to) {
        $keyword = '%' . $keyword . '%';
        $db = $this->load->database('default', TRUE);
        $a = array();
        $sql = "select count(adv_id) c FROM adv2013 where 1=1 ";
        if ($keyword != "%Nội dung tìm%" && $keyword != "%%") {
            $sql.=" and adv2013.title like N? ";
            $a[] = $keyword;
        }
        if ($cate_id != "") {
            $sql.=" and adv2013.adv_cate_id=? ";
            $a[] = $cate_id;
        }
        if ($city_id != "") {
            $sql.=" and adv2013.city_id=? ";
            $a[] = $city_id;
        }
        $sql.=" and adv2013.status = '1';";
        $query = $db->query($sql, $a);
        $aRows = $query->result();
        $iRow_Count = $aRows[0]->c;

        $sql = "SELECT adv2013.*, adv2013.city_id, adv_cate.cate_name from adv2013
						inner join adv_cate on adv2013.adv_cate_id=adv_cate.cate_id 
						where 1=1";
        if ($keyword != "%Nội dung tìm%" && $keyword != "%%") {
            $sql.=" and adv2013.title like N? ";
        }
        if ($cate_id != "") {
            $sql.=" and adv2013.adv_cate_id=? ";
        }
        if ($city_id != "") {
            $sql.=" and adv2013.city_id=? ";
        }
        $sql.=" and adv2013.status = '1' order by adv_id DESC LIMIT ?,?;";
        $a[] = $limit_from;
        $a[] = $limit_to;
        $query = $db->query($sql, $a);
        $result = $query->result();
        return array($iRow_Count, $result);
    }

    //dùng cho trang Rao Vặt
    function _get_adv_list_bycate($cate_id, $limit_from, $limit_to) {
        $db = $this->load->database('default', TRUE);
        $sql = "SELECT adv_id, url, title, `desc`, create_date, city_id FROM adv2013
						WHERE adv_cate_id = ? and status='1'
		 			  ORDER BY adv_id DESC LIMIT {$limit_from},{$limit_to};";
        $query = $db->query($sql, array($cate_id));
        return $query->result();
    }

    function _get_adv_list($id, $limit_from, $limit_to) {
        $db = $this->load->database('default', TRUE);
        $sql = "SELECT adv_id, url, title, create_date, city_id FROM adv2013
                WHERE adv_id <> {$id}
		ORDER BY adv_id DESC LIMIT {$limit_from},{$limit_to};";
        $query = $db->query($sql);
        return $query->result();
    }

    function _get_adv_nopaging($limit_start, $limit_count) {
        $db = $this->load->database('default', TRUE);
        $sql = "SELECT url, adv_id, title from adv2013 where status='1' order by adv_id desc LIMIT ?,?;";
        $query = $db->query($sql, array($limit_start, $limit_count));
        return array($query->result());
    }

    function get_adv_detail_by_id($id) {
        $db = $this->load->database('default', TRUE);
        $sql = "SELECT * from adv2013 where status='1' and adv_id=?;";
        $query = $db->query($sql, $id);
        return $query->result();
    }

    function _get_adv_cate_list() {
        $db = $this->load->database('default', TRUE);
        $sql = "SELECT * FROM adv_cate";
        $query = $db->query($sql);
        return $query->result();
    }

    function _get_city_list() {
        $city1 = new stdClass();
        $city1->city_id = 1;
        $city1->city_name = "Toàn quốc";
        $array[] = $city1;
        $city2 = new stdClass();
        $city2->city_id = 2;
        $city2->city_name = "Miền Nam";
        $array[] = $city2;
        $city3 = new stdClass();
        $city3->city_id = 3;
        $city3->city_name = "Miền Trung";
        $array[] = $city3;
        $city = new stdClass();
        $city->city_id = 4;
        $city->city_name = "Miền Bắc";
        $array[] = $city;

        return $array;
    }

    function _insert_adv($tablename, $data) {
        //$sql = $this->db->query("SET NAMES 'utf8'");
        $db = $this->load->database('default', TRUE);
        $db->insert($tablename, $data);
        return $db->insert_id();
    }

    // hàm nay để test nested array
    function do_dump(&$var, $var_name = NULL, $indent = NULL, $reference = NULL) {
        $do_dump_indent = "<span style='color:#eeeeee;'>|</span> &nbsp;&nbsp; ";
        $reference = $reference . $var_name;
        $keyvar = 'the_do_dump_recursion_protection_scheme';
        $keyname = 'referenced_object_name';

        if (is_array($var) && isset($var[$keyvar])) {
            $real_var = &$var[$keyvar];
            $real_name = &$var[$keyname];
            $type = ucfirst(gettype($real_var));
            echo "$indent$var_name <span style='color:#a2a2a2'>$type</span> = <span style='color:#e87800;'>&amp;$real_name</span><br>";
        } else {
            $var = array($keyvar => $var, $keyname => $reference);
            $avar = &$var[$keyvar];

            $type = ucfirst(gettype($avar));
            if ($type == "String")
                $type_color = "<span style='color:green'>";
            elseif ($type == "Integer")
                $type_color = "<span style='color:red'>";
            elseif ($type == "Double") {
                $type_color = "<span style='color:#0099c5'>";
                $type = "Float";
            } elseif ($type == "Boolean")
                $type_color = "<span style='color:#92008d'>";
            elseif ($type == "NULL")
                $type_color = "<span style='color:black'>";

            if (is_array($avar)) {
                $count = count($avar);
                echo "$indent" . ($var_name ? "$var_name => " : "") . "<span style='color:#a2a2a2'>$type ($count)</span><br>$indent(<br>";
                $keys = array_keys($avar);
                foreach ($keys as $name) {
                    $value = &$avar[$name];
                    $this->do_dump($value, "['$name']", $indent . $do_dump_indent, $reference);
                }
                echo "$indent)<br>";
            } elseif (is_object($avar)) {
                echo "$indent$var_name <span style='color:#a2a2a2'>$type</span><br>$indent(<br>";
                foreach ($avar as $name => $value)
                    $this->do_dump($value, "$name", $indent . $do_dump_indent, $reference);
                echo "$indent)<br>";
            } elseif (is_int($avar))
                echo "$indent$var_name = <span style='color:#a2a2a2'>$type(" . strlen($avar) . ")</span> $type_color$avar</span><br>";
            elseif (is_string($avar))
                echo "$indent$var_name = <span style='color:#a2a2a2'>$type(" . strlen($avar) . ")</span> $type_color\"$avar\"</span><br>";
            elseif (is_float($avar))
                echo "$indent$var_name = <span style='color:#a2a2a2'>$type(" . strlen($avar) . ")</span> $type_color$avar</span><br>";
            elseif (is_bool($avar))
                echo "$indent$var_name = <span style='color:#a2a2a2'>$type(" . strlen($avar) . ")</span> $type_color" . ($avar == 1 ? "TRUE" : "FALSE") . "</span><br>";
            elseif (is_null($avar))
                echo "$indent$var_name = <span style='color:#a2a2a2'>$type(" . strlen($avar) . ")</span> {$type_color}NULL</span><br>";
            else
                echo "$indent$var_name = <span style='color:#a2a2a2'>$type(" . strlen($avar) . ")</span> $avar<br>";

            $var = $var[$keyvar];
        }
    }

}

?>